package view.cloudFactory;

import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXTextField;
import controller.EquipMsgController;
import dao.EquipMsgDao;
import dao.ProductMsgDao;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import pojo.equip.EquipMsgEntity;
import pojo.product.AddProductProperty;
import pojo.product.ProductMsgEntity;
import view.Main;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

/**
 * @author Limohan
 */
public class AddProductController {
    private Main myApp;
    private static EquipMsgDao equipMsgDao = new EquipMsgDao();
    private static ProductMsgDao productMsgDao = new ProductMsgDao();

    private String id;

    public void setApp(Main myApp) {
        this.myApp = myApp;
    }

    public void setId(String id) {
        this.id = id;
    }

    public AddProductController() {
    }

    @FXML
    private TableView<AddProductProperty> productTable;

    @FXML
    private TableColumn<AddProductProperty, String> productNameCol;

    @FXML
    private TableColumn<AddProductProperty, String> efficiencyCol;

    @FXML
    private TextField equipCode;

    @FXML
    private TextField equipName;

    @FXML
    private JFXComboBox<String> productName;

    @FXML
    private JFXTextField efficiency;

    @FXML
    void initialize() throws IOException {
        for (ProductMsgEntity productMsgEntity : productMsgDao.findMsgList()) {
            productName.getItems().add(productMsgEntity.getName());
        }
    }

    @FXML
    void addProduct(ActionEvent event) throws IOException {
        equipMsgDao.addProductMap(id,productName.getValue(),efficiency.getText());
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setHeaderText("!");
        alert.setContentText("添加成功");
        alert.showAndWait();
        this.displayInfo(equipMsgDao.findMsg(id));
}

    @FXML
    void returnLastPage(ActionEvent event) throws IOException {
        myApp.goToCloudFactoryManagePage();
    }

    public void originDisplay() throws IOException {
        equipCode.setText(equipMsgDao.findMsg(id).getCode());
        equipName.setText(equipMsgDao.findMsg(id).getName());
        this.displayInfo(equipMsgDao.findMsg(id));

    }
    void displayInfo(EquipMsgEntity equipMsgEntity){
        productTable.getItems();
        ObservableList<AddProductProperty> products = FXCollections.observableArrayList();
        if(equipMsgEntity.getProductMap()!=null){
            Map<String,String> map = equipMsgEntity.getProductMap();
            Set<String> KeySet = map.keySet();

            for (String str : KeySet) {
                    AddProductProperty addProductProperty = new AddProductProperty();
                    addProductProperty.setName(str);
                    addProductProperty.setEfficiency(map.get(str));
                    products.add(addProductProperty);
                }
            productNameCol.setCellValueFactory(param -> param.getValue().nameProperty());
            efficiencyCol.setCellValueFactory(param -> param.getValue().efficiencyProperty());
            productTable.setItems(products);
            }else{
            productTable.setItems(FXCollections.emptyObservableList());
        }
    }
}